package cn.itcast.xuesheng.web.servlet;

import cn.itcast.xuesheng.domain.PageBean;
import cn.itcast.xuesheng.domain.Topic;
import cn.itcast.xuesheng.domain.User;
import cn.itcast.xuesheng.service.TopicService;
import cn.itcast.xuesheng.service.impl.TopicServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

//@WebServlet("/topic/*")
@Controller
@RequestMapping("/topic")
@SessionAttributes("user")
public class TopicServlet extends BaseServlet {
    //    private TopicServiceImpl topicService=new TopicServiceImpl();
    private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

    //    protected void pageQueryForcid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    @RequestMapping("/pageQueryForcid")
    public @ResponseBody
    PageBean pageQueryForcid(@RequestParam("cid") String cidStr, @RequestParam("currentPage") String currentPageStr, String title) {
//        String currentPageStr = request.getParameter("currentPage");
//        String pageSizeStr = request.getParameter("pageSize");
//        String cidStr = request.getParameter("cid");
//        String title = request.getParameter("title");
        String pageSizeStr = null;
        if ("null".equals(title)) {
            title = "";
        }
        int currentPage = 0;
        if (currentPageStr != null && currentPageStr.length() > 0) {
            currentPage = Integer.parseInt(currentPageStr);
        } else {
            currentPage = 1;
        }
        int pageSize = 0;
        if (pageSizeStr != null && pageSizeStr.length() > 0) {
            pageSize = Integer.parseInt(pageSizeStr);
        } else {
            pageSize = 20;
        }
        int cid = 0;
        if (cidStr != null && cidStr.length() > 0 && !"null".equals(cidStr)) {
            cid = Integer.parseInt(cidStr);
        }
//        TopicServiceImpl topicService=new TopicServiceImpl();
        TopicService topicService1 = (TopicService) ac.getBean("topicService");//通过id找到全限定类
        PageBean<Topic> pageBean = topicService1.pageQuery(cid, currentPage, pageSize, title);
        System.out.println(cid);
        System.out.println(currentPage);
        System.out.println(pageSize);
//        writeValue(response,pageBean);
        return pageBean;
    }

    //    protected void pageQueryForUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    @RequestMapping("/pageQueryForUser")
    public @ResponseBody
    PageBean pageQueryForUser(ModelMap modelMap, String currentPage) {
//        String currentPageStr = request.getParameter("currentPage");
//        String pageSizeStr = request.getParameter("pageSize");
//        User user = (User) request.getSession().getAttribute("user");
        String pageSizeStr = null;
        User user = (User) modelMap.get("user");
        int currentPage1 = 0;
        if (currentPage != null && currentPage.length() > 0) {
            currentPage1 = Integer.parseInt(currentPage);
        } else {
            currentPage1 = 1;
        }
        int pageSize = 0;
        if (pageSizeStr != null && pageSizeStr.length() > 0) {
            pageSize = Integer.parseInt(pageSizeStr);
        } else {
            pageSize = 10;
        }
//        TopicServiceImpl topicService=new TopicServiceImpl();
        TopicService topicService1 = (TopicService) ac.getBean("topicService");
        PageBean<Topic> pageBean = topicService1.pageQueryForUsername(user.getUsername(), currentPage1, pageSize);
//        writeValue(response,pageBean);
        return pageBean;
    }

    //    protected void clickthumb(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    @RequestMapping("/clickthumb")
    public void clickthumb(String num, String tid) {
//        String tid = request.getParameter("tid");
//        String num = request.getParameter("num");
//        TopicService topicService=new TopicServiceImpl();
        TopicService topicService = (TopicService) ac.getBean("topicService");
        topicService.update(num, tid);
    }

    //    protected void queryforxue(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    @RequestMapping("/queryforxue")
    public @ResponseBody
    List queryforxue() {
        TopicService topicService1 = (TopicService) ac.getBean("topicService");
        List<Topic> queryforxue = topicService1.queryforxue();
//        writeValue(response,queryforxue);
        return queryforxue;
    }

    //    protected void queryforxue2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    @RequestMapping("/queryforxue2")
    public @ResponseBody
    List queryforxue2() {
        TopicService topicService1 = (TopicService) ac.getBean("topicService");
        List<Topic> queryforxue = topicService1.queryforxue2();
//        writeValue(response,queryforxue);
        return queryforxue;
    }

    //    protected void queryfortu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    @RequestMapping("/queryfortu")
    public @ResponseBody
    List queryfortu() {
        TopicService topicService1 = (TopicService) ac.getBean("topicService");
        List<Topic> queryfortu = topicService1.queryfortu();
//        writeValue(response,queryfortu);
        return queryfortu;
    }

    //    protected void queryfortu2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    @RequestMapping("/queryfortu2")
    public @ResponseBody
    List queryfortu2() {
        TopicService topicService1 = (TopicService) ac.getBean("topicService");
        List<Topic> queryfortu2 = topicService1.queryfortu2();
//        writeValue(response,queryfortu2);
        return queryfortu2;
    }

    //    protected void queryfortnannv(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    @RequestMapping("/queryfornannv")
    public @ResponseBody
    List queryfornannv() {
        TopicService topicService1 = (TopicService) ac.getBean("topicService");
        List<Topic> queryfortnannv = topicService1.queryfortnanv();
//        writeValue(response,queryfortnannv);
        return queryfortnannv;
    }

    //    protected void queryforimg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    @RequestMapping("/queryforimg")
    public @ResponseBody
    List queryforimg() {
        TopicService topicService1 = (TopicService) ac.getBean("topicService");
        List<Topic> queryforimg = topicService1.queryforimg();
//        writeValue(response,queryforimg);
        return queryforimg;
    }

    //    protected void queryforxue3(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    @RequestMapping("/queryforxue3")
    public @ResponseBody
    List queryforxue3() {
        TopicService topicService1 = (TopicService) ac.getBean("topicService");
        List<Topic> queryforxue3 = topicService1.queryforxue3();
//        writeValue(response,queryforxue3);
        return queryforxue3;
    }

    //    protected void queryforx4(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    @RequestMapping("/queryforx4")
    public @ResponseBody
    List queryforx4() {
        TopicService topicService1 = (TopicService) ac.getBean("topicService");
        List<Topic> queryforx4 = topicService1.queryforx4();
//        writeValue(response,queryforx4);
        return queryforx4;
    }

    //    protected void insertcontext(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    @RequestMapping("/insertcontext")
    public void insertcontext(String cid, String title, String context, ModelMap modelMap, MultipartFile uploads) throws IOException {
        String path = "F:/XueshengMaven/src/main/webapp/img";
        // 判断，该路径是否存在
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        String filename = uploads.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        filename = uuid + "_" + filename;
        uploads.transferTo(new File(path, filename));

        String imgcontext="img/"+filename;

        User user = (User) modelMap.get("user");
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = new Date();
        String date = df.format(date1);
        String cname = null;
        switch (cid) {
            case "1":
                cname = "学术圈";
                break;
            case "2":
                cname = "吐槽会";
                break;
            case "3":
                cname = "男女那些事";
                break;
            case "4":
                cname = "废品重用";
                break;
            case "5":
                cname = "学术资源分享";
                break;
            case "6":
                cname = "下课后";
                break;
        }
//       TopicServiceImpl topicService=new TopicServiceImpl();
        TopicService topicService1 = (TopicService) ac.getBean("topicService");
        topicService1.insertcontext(user.getUsername(), title, context, cid, cname, date,imgcontext);

    }

//    @RequestMapping("/insertimg")
//    public void insertimg(HttpServletRequest request, MultipartFile uploads) throws Exception {
//        String path = "F:/XueshengMaven/src/main/webapp/img";
//        System.out.println(uploads);
//        // 判断，该路径是否存在
//        File file = new File(path);
//        if (!file.exists()) {
//            file.mkdirs();
//        }
//        String filename = uploads.getOriginalFilename();
//        String uuid = UUID.randomUUID().toString().replace("-", "");
//        filename = uuid + "_" + filename;
//        uploads.transferTo(new File(path, filename));
//    }
}
